package com.alex.utils.html;

import java.util.Hashtable;

public class Test {
	public static void main(String[] args) {

		String tag = " type   =t==ext===   name= \"user\" value=\"pedro \"''''  readonly selected";

		Hashtable<String, String> atributes = new Hashtable<String, String>();

		int p1 = 0;
		int len = tag.length();
		int tokenBegin = -1;

		String currentParam = null;
		boolean value = false;
		boolean end = false;

		for (;;) {

			if (end)
				break;

			char c;
			if (p1 != len) {
				c = tag.charAt(p1);
			} else {
				c = ' ';
				end = true;
			}

			if (tokenBegin < 0 && c == '\"') {

				int p2 = tag.indexOf('\"', p1 + 1);
				if (p2 < 0)
					break;
				String token = tag.substring(p1 + 1, p2);

				if (value && currentParam != null)
					atributes.put(currentParam, token);
				currentParam = null;
				value = false;

				print(token);
				p1 = p2;

			} else if (tokenBegin < 0 && c == '\'') {

				int p2 = tag.indexOf('\'', p1 + 1);
				if (p2 < 0)
					break;
				String token = tag.substring(p1 + 1, p2);

				if (value && currentParam != null)
					atributes.put(currentParam, token);
				currentParam = null;
				value = false;

				print(token);
				p1 = p2;

			} else if (Character.isWhitespace(c) || c == '=') {

				if (c == '=')
					print(c);

				if (c == '=' && value) {
					p1++;
					continue;
				}

				if (tokenBegin >= 0) {

					String token = tag.substring(tokenBegin, p1);
					print(token);
					tokenBegin = -1;

					if (value) {

						if (currentParam != null) {
							atributes.put(currentParam, token);
							currentParam = null;
							value = false;
						}

					} else if (currentParam != null) {

						atributes.put(currentParam, "");
						currentParam = token;

					} else
						currentParam = token;
				}

				if (c == '=')
					value = true;

			} else if (tokenBegin < 0)
				tokenBegin = p1;

			p1++;
		}

		if (currentParam != null)
			atributes.put(currentParam, "");

		System.exit(1);
	}

	private static void print(String s) {
		System.out.println("------------------------------------------");
		System.out.println(s);
	}

	private static void print(char s) {
		System.out.println("------------------------------------------");
		System.out.println(s);
	}
}
